#include "CErrorHandlingSys.h"
#include "CLogFile.h"

#include <windows.h>
#include <shlwapi.h>

lvVoid CErrorHandlingSys::Create(const lvString &cstrConfigurationFile)
{
	m_pInstance = new CErrorHandlingSys(cstrConfigurationFile);
};

CErrorHandlingSys::CErrorHandlingSys(const lvString &cstrConfigurationFile)
{
	lvChar _chBuffer[64];

	GetPrivateProfileString(COLOR_SECTION, CRIT_ERRORCOLOR_KEY, _S("\0"), _chBuffer, 64, cstrConfigurationFile.c_str());
	if(!StrToIntEx(_chBuffer, STIF_SUPPORT_HEX, (int*)&m_dwCritErrTxtColor))
	{
		MessageBox(0, _S("Error parsing error handling configuration file!"), _S("Critical error!"), MB_OK|MB_ICONSTOP);
		ExitProcess(0);
	};

	GetPrivateProfileString(COLOR_SECTION, ERROR_COLOR_KEY, _S("\0"), _chBuffer, 64, cstrConfigurationFile.c_str());
	if(!StrToIntEx(_chBuffer, STIF_SUPPORT_HEX, (int*)&m_dwErrTxtColor))
	{
		MessageBox(0, _S("Error parsing error handling configuration file!"), _S("Critical error!"), MB_OK|MB_ICONSTOP);
		ExitProcess(0);
	};

	GetPrivateProfileString(COLOR_SECTION, WARNING_COLOR_KEY, _S("\0"), _chBuffer, 64, cstrConfigurationFile.c_str());
	if(!StrToIntEx(_chBuffer, STIF_SUPPORT_HEX, (int*)&m_dwWarningTxtColor))
	{
		MessageBox(0, _S("Error parsing error handling configuration file!"), _S("Critical error!"), MB_OK|MB_ICONSTOP);
		ExitProcess(0);
	};

	GetPrivateProfileString(COLOR_SECTION, DEFAULT_COLOR_NAME, _S("\0"), _chBuffer, 64, cstrConfigurationFile.c_str());
	if(!StrToIntEx(_chBuffer, STIF_SUPPORT_HEX, (int*)&m_dwDefTxtColor))
	{
		MessageBox(0, _S("Error parsing error handling configuration file!"), _S("Critical error!"), MB_OK|MB_ICONSTOP);
		ExitProcess(0);
	};
//---------------------------------------------------------
	GetPrivateProfileString(LOGFILENAMES_SECTION, GRAPHICS_KEY, _S("\0"), _chBuffer, 64, cstrConfigurationFile.c_str());
	m_arLogs[SUBSYSTEM_ID_GRAPHICS] = CreateFileLog(lvString(_chBuffer));
	if(!m_arLogs[SUBSYSTEM_ID_GRAPHICS])
	{
		MessageBox(0, _S("Error creating log streams!"), _S("Critical error!"), MB_OK|MB_ICONSTOP);
		ExitProcess(0);
	};

	GetPrivateProfileString(LOGFILENAMES_SECTION, DEFAULT_KEY, _S("\0"), _chBuffer, 64, cstrConfigurationFile.c_str());
	m_arLogs[SUBSYSTEM_ID_DEFAULT] = CreateFileLog(lvString(_chBuffer));
	if(!m_arLogs[SUBSYSTEM_ID_DEFAULT])
	{
		MessageBox(0, _S("Error creating log streams!"), _S("Critical error!"), MB_OK|MB_ICONSTOP);
		ExitProcess(0);
	};

	GetPrivateProfileString(LOGFILENAMES_SECTION, SOUND_KEY, _S("\0"), _chBuffer, 64, cstrConfigurationFile.c_str());
	m_arLogs[SUBSYSTEM_ID_SOUND] = CreateFileLog(lvString(_chBuffer));
	if(!m_arLogs[SUBSYSTEM_ID_SOUND])
	{
		MessageBox(0, _S("Error creating log streams!"), _S("Critical error!"), MB_OK|MB_ICONSTOP);
		ExitProcess(0);
	};

	GetPrivateProfileString(LOGFILENAMES_SECTION, INPUT_KEY, _S("\0"), _chBuffer, 64, cstrConfigurationFile.c_str());
	m_arLogs[SUBSYSTEM_ID_INPUT] = CreateFileLog(lvString(_chBuffer));
	if(!m_arLogs[SUBSYSTEM_ID_INPUT])
	{
		MessageBox(0, _S("Error creating log streams!"), _S("Critical error!"), MB_OK|MB_ICONSTOP);
		ExitProcess(0);
	};
};
CErrorHandlingSys::~CErrorHandlingSys()
{
	for(int i = 0; i<SUBSYSTEM_NUM; i++)
	{
		delete m_arLogs[i];
	};
};

lvVoid CErrorHandlingSys::_CriticalError(const lvString &cstrDescription, const lvInt ciSubSys_ID)
{
	m_arLogs[ciSubSys_ID]->SetTxtColor(m_dwCritErrTxtColor);
	m_arLogs[ciSubSys_ID]->SetTxtBold(true);
	m_arLogs[ciSubSys_ID]->SetTxtItallic(false);

	m_arLogs[ciSubSys_ID]->WriteLn(cstrDescription);

	ExitProcess(0);
};
lvVoid CErrorHandlingSys::_Error(const lvString &cstrDescription, const lvInt ciSubSys_ID)
{
	m_arLogs[ciSubSys_ID]->SetTxtColor(m_dwErrTxtColor);
	m_arLogs[ciSubSys_ID]->SetTxtBold(true);
	m_arLogs[ciSubSys_ID]->SetTxtItallic(false);

	m_arLogs[ciSubSys_ID]->WriteLn(cstrDescription);
};
lvVoid CErrorHandlingSys::_Warning(const lvString &cstrDescription, const lvInt ciSubSys_ID)
{
	m_arLogs[ciSubSys_ID]->SetTxtColor(m_dwWarningTxtColor);
	m_arLogs[ciSubSys_ID]->SetTxtBold(false);
	m_arLogs[ciSubSys_ID]->SetTxtItallic(false);

	m_arLogs[ciSubSys_ID]->WriteLn(cstrDescription);
};
	
lvVoid CErrorHandlingSys::_TraceMessage(const lvString &cstrMessage, const lvDword cdwColor)
{
	if(cdwColor==USE_DEFAULT_COLOR)
	{
		m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetTxtColor(m_arLogs[SUBSYSTEM_ID_DEFAULT]->GetDefTxtColor());
	}
	else
	{
		m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetTxtColor(cdwColor);
	};

	m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetFont(m_arLogs[SUBSYSTEM_ID_DEFAULT]->GetDefFont());
	m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetFontSize(m_arLogs[SUBSYSTEM_ID_DEFAULT]->GetDefFontSize());
	m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetTxtBold(m_arLogs[SUBSYSTEM_ID_DEFAULT]->GetDefTxtBold());
	m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetTxtItallic(m_arLogs[SUBSYSTEM_ID_DEFAULT]->GetDefTxtItallic());

	m_arLogs[SUBSYSTEM_ID_DEFAULT]->WriteLn(cstrMessage);
};

lvVoid CErrorHandlingSys::_OperationExecuted(const lvString &cstrOperationName, const lvString &cstrFilename, const lvInt ciLine)
{
	lvChar _chBuffer[64];
	_stprintf(_chBuffer, _S("%d"), ciLine);

	lvString _strResultMessage = cstrOperationName + lvString(_S("executed. File: ")) + cstrFilename + lvString(_S("."));
	if(ciLine!=0)
	{
		_strResultMessage+=lvString(_S(". File: ")) + cstrFilename + lvString(_S(". Line: ")) + lvString(_chBuffer);
	};

	m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetTxtColor(m_arLogs[SUBSYSTEM_ID_DEFAULT]->GetDefTxtColor());
	m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetFont(m_arLogs[SUBSYSTEM_ID_DEFAULT]->GetDefFont());
	m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetFontSize(m_arLogs[SUBSYSTEM_ID_DEFAULT]->GetDefFontSize());
	m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetTxtBold(m_arLogs[SUBSYSTEM_ID_DEFAULT]->GetDefTxtBold());
	m_arLogs[SUBSYSTEM_ID_DEFAULT]->SetTxtItallic(m_arLogs[SUBSYSTEM_ID_DEFAULT]->GetDefTxtItallic());

	m_arLogs[SUBSYSTEM_ID_DEFAULT]->Write(_strResultMessage);
};
lvVoid CErrorHandlingSys::_OperationExit(const lvDword cdwStatus)
{
};